import java.util.HashMap;
import java.util.Map;

public class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> window = new HashMap<>();
        int left = 0,right = 0;
        int longestLength = 0;
        while (right < s.length()){
            char c = s.charAt(right);
            right++;
            window.put(c,window.getOrDefault(c,0)+1);
            while (window.get(c) > 1){
                char d = s.charAt(left);
                left++;
                window.put(d,window.get(d) - 1);
            }
            longestLength = Math.max(longestLength,(right - left));
        }
        return longestLength;
    }
}
